<template>
  <div class="shopcar-container">
      <div class="shop-item">
        <div class="mui-card" v-for="(item,i) in shopCarList" :key="item.id">
          <div class="mui-card-content">
            <div class="mui-card-content-inner">
              <mt-switch v-model="$store.getters.getSelected[item.id]"
                @change="changeSelected(item.id)"></mt-switch>
              <img src="@/assets/menu1.png">
              <div class="info">
                <h1>{{item.title}}</h1>
                <p>
                  <span>¥{{item.price}}</span>
                  <numbox :initcount="$store.getters.getCount[item.id]" :id="item.id"></numbox>
                  <a href="#" @click.prevent="removeFromCar(item.id,i)">删除</a>
                </p>
              </div>
            </div>
          </div>
			  </div>
        <div class="mui-card">
          <div class="mui-card-content">
            <div class="mui-card-content-inner sum">
              <div class="left">
                <p>总计(不含运费)</p>
                <p>已勾选商品<span class="red"> {{$store.getters.getSum.count}} </span>件，总价<span class="red"> ¥{{$store.getters.getSum.sum}} </span></p>
              </div>
              <mt-button type="danger">去结算</mt-button>
            </div>
          </div>
			  </div>
      </div>
  </div>
</template>

<script>
import { Toast } from 'mint-ui'
import numbox from '@/components/subcomponents/shopcar_numbox'
export default {
  data(){
    return{
      shopCarList:[]
    }
  },
  created(){
    this.getShopCar()
  },
  methods:{
    async getShopCar(){
      var shopcar = []
      this.$store.state.car.forEach(item => shopcar.push(item.id));
      if(shopcar.length<=0)
        return;
      const {data} = await this.$http.get('api/goods/getshopcarlist/'+shopcar.join(','))
      if(data.status == 0){
          this.shopCarList = data.message
      }else{
          // Toast('加载购物车失败。。。')
          this.shopCarList = this.$store.state.car
      }
    },
    removeFromCar(id,index){
      this.shopCarList.splice(index,1)
      this.$store.commit('removeFromCar',id)
    },
    changeSelected(id){
      this.$store.commit('changeSelected',id)

    }
  },
  components:{
    numbox
  }
}
</script>
<style lang="scss" scoped>
.shopcar-container{
  background-color: #eee;
  overflow: hidden;
  .mui-card-content-inner{
    display: flex;
    align-items: center;
    img{
      width: 45px;
      height: 45px;
    }
    .info{
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      h1{
        font-size: 13px;
        font-weight: bold;
      }
      span{
        color: red;
        font-size: 14px;
        font-weight: bold;
      }
    }
  }
  .sum{
    display: flex;
    justify-content: space-between;
    .red{
      color: red;
      font-weight: bolder;
      font-size: 16px;
    }
  }
 
}
</style>

